package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.annotation.LogRecord;
import com.baizhi.entity.PmsBrand;
import com.baizhi.enums.LogTypeEnum;
import com.baizhi.service.PmsBrandService;
import com.baizhi.vo.PmsBrandUpdateVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yu
 */
@RestController
public class PmsBrandController {
    @Autowired
    private PmsBrandService pmsBrandService;
    @LogRecord(type = LogTypeEnum.SELECT,content = "查询全部品牌")
    @RequestMapping("/pmsBrands/showAll")
    public Map showAll(Integer page,Integer limit){
        Map map = pmsBrandService.showAll(page, limit);
        return map;
    }

   /* @LogRecord(type = LogTypeEnum.INSERT,content = "添加品牌")
    @PostMapping("/pmsBrands")
    public Map insert(@RequestBody PmsBrand pmsBrand){
        HashMap<String, Object> map = new HashMap<>();
        pmsBrandService.insert(pmsBrand);
        map.put("status","success");
        return map;
    }*/
    @LogRecord(type = LogTypeEnum.DELETE,content = "删除品牌")
    @DeleteMapping("/pmsBrands/{id}")
    public Map delete(@PathVariable("id")Integer id){
        HashMap<String, String> map = new HashMap<>();
        pmsBrandService.delete(id);
        map.put("status","success");
        return map;
    }
    @LogRecord(type = LogTypeEnum.UPDATE,content = "修改品牌")
    @PutMapping("/pmsBrands")
    public Map update(@RequestBody PmsBrandUpdateVo pmsBrandUpdateVo){
        HashMap<String, Object> map = new HashMap<>();
        pmsBrandService.update(pmsBrandUpdateVo);
        return map;
    }

    @RequestMapping("/uploadExcel")
    public Map uploadExcel(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        pmsBrandService.uploadExcel(inputStream);
        HashMap<String, String> map = new HashMap<>();
        map.put("status","success");
        return map;

    }
    @RequestMapping("/downloadExcel")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        Workbook sheets = pmsBrandService.downloadExcel();
        response.setHeader(("Content-Disposition"),"attachment;fileName=" + URLEncoder.encode("下载信息.xls","UTF-8"));
        response.setContentType("application/vnd.ms-excel");
        sheets.write(response.getOutputStream());
    }
}
